﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyLiveMesh.Web;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;

namespace MyLiveMesh.ViewModels
{
    public class EditAccount : Abstract
    {
        string m_username;
        string m_password;
        string m_confirmPassword;
        string m_email;
        string m_securityQuestion;
        string m_securityAnswer;
        bool m_updateButtonEnabled;
        bool m_updateHasError;
        string m_updateErrorMessage;

        public string Username
        {
            get
            {
                return (m_username);
            }

            set
            {
                if (m_username != value)
                {
                    m_username = value;
                    this.OnPropertyChanged("Username");
                }
            }
        }

        public string Password
        {
            get
            {
                return (m_password);
            }

            set
            {
                if (m_password != value)
                {
                    m_password = value;
                    this.OnPropertyChanged("Password");
                }
            }
        }

        public string ConfirmPassword
        {
            get
            {
                return (m_confirmPassword);
            }

            set
            {
                if (m_confirmPassword != value)
                {
                    m_confirmPassword = value;
                    this.OnPropertyChanged("ConfirmPassword");
                }
            }
        }

        public string Email
        {
            get
            {
                return (m_email);
            }

            set
            {
                if (m_email != value)
                {
                    m_email = value;
                    this.OnPropertyChanged("Email");
                }
            }
        }

        public string SecurityQuestion
        {
            get
            {
                return (m_securityQuestion);
            }

            set
            {
                if (m_securityQuestion != value)
                {
                    m_securityQuestion = value;
                    this.OnPropertyChanged("SecurityQuestion");
                }
            }
        }

        public string SecurityAnswer
        {
            get
            {
                return (m_securityAnswer);
            }

            set
            {
                if (m_securityAnswer != value)
                {
                    m_securityAnswer = value;
                    this.OnPropertyChanged("SecurityAnswer");
                }
            }
        }

        public bool UpdateButtonEnabled
        {
            get
            {
                return (m_updateButtonEnabled);
            }

            set
            {
                if (m_updateButtonEnabled != value)
                {
                    m_updateButtonEnabled = value;
                    this.OnPropertyChanged("UpdateButtonEnabled");
                }
            }
        }

        public bool UpdateHasError
        {
            get
            {
                return (m_updateHasError);
            }

            set
            {
                if (m_updateHasError != value)
                {
                    m_updateHasError = value;
                    this.OnPropertyChanged("UpdateHasError");
                }
            }
        }

        public string UpdateErrorMessage
        {
            get
            {
                return (m_updateErrorMessage);
            }

            set
            {
                if (m_updateErrorMessage != value)
                {
                    m_updateErrorMessage = value;
                    this.OnPropertyChanged("UpdateErrorMessage");
                }
            }
        }

        public EditAccount()
        {
            UpdateButtonEnabled = true;
        }

        public ICommand CommandEditAccount
        {
            get
            {
                return (new EditAccountCommand(this));
            }
        }
    }

    public class EditAccountCommand : ICommand
    {
        EditAccount m_edit;

        public EditAccountCommand(EditAccount _edit)
        {
            m_edit = _edit;
        }

        #region ICommand Membres

        public bool CanExecute(object parameter)
        {
            return (true);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            m_edit.UpdateButtonEnabled = false;
            
            //user.UserState.
            RegistrationDomainContext context = new RegistrationDomainContext();
            /*MyLiveMesh.Web.Models.RegisterData data = new MyLiveMesh.Web.Models.RegisterData();
            try
            {
                data.UserName = m_register.Username;
                data.Password = m_register.Password;
                data.Email = m_register.Email;
                data.ConfirmPassword = m_register.ConfirmPassword;
                data.SecurityQuestion = m_register.SecurityQuestion;
                data.SecurityAnswer = m_register.SecurityAnswer;
                context.RegisterDatas.Add(data);

                context.SubmitChanges(registerCompleted_event, null);
            }
            catch (ValidationException ve)
            {
                m_register.RegisterHasError = true;
                m_register.RegisterErrorMessage = ve.Message;
                m_register.RegisterButtonEnabled = true;
            }*/
        }

        private void registerCompleted_event(SubmitOperation so)
        {
            /*if (m_register.RegisterHasError = so.HasError)
            {
                m_register.RegisterErrorMessage = "";
                var l = so.EntitiesInError;
                foreach (var en in l)
                {
                    var errors = en.ValidationErrors;
                    foreach (var error in errors)
                    {
                        m_register.RegisterErrorMessage += error.ErrorMessage + ", ";
                    }
                }
                so.MarkErrorAsHandled();
            }
            else
            {
                m_register.Username = "";
                m_register.Password = "";
                m_register.ConfirmPassword = "";
                m_register.Email = "";
                m_register.SecurityQuestion = "";
                m_register.SecurityAnswer = "";
            }
            m_register.RegisterButtonEnabled = true;*/
        }

        #endregion
    }
}
